import {
  get,
  add
} from "../../utils/db";
import {
  multiUpload
} from "../../utils/tools";
const app=getApp();
Page({
  data: {
    typeList: [], //菜谱分类
    files: [], //图片临时地址
  },
  async onLoad() {
    //读取type集合
    var result = await get("type").catch(err => console.error(err))
    // console.log(result)
    this.setData({
      typeList: result.data
    })
  },
  //选择本地图片
  handleSelect(e) {
    var temp = e.detail.tempFilePaths;
    var files = temp.map(item => {
      return {
        url: item
      }
    })
    this.setData({
      files
    })
  },
  //提交表单
  async mySubmit(e) {
    console.log(e)
    let {nickName,avatarUrl}=app.globalData.userInfo;
    let {name,typeid,info}=e.detail.value;


    //1 上传图片
    // return;
    var result = await multiUpload(this.data.files).catch(err => {
      console.log(err)
      return;
    })
    // console.log(result);return;
    var images=result.map(item=>{
      return item.fileID
    })
    //2 添加数据库
    var addtime=new Date().getTime()
    var formData={name,typeid,info,nickName,avatarUrl,views:0,likes:0,status:1,addtime,images}
   var result =await  add("menu",formData).catch(err=>{
      console.log(err);
      return;
    })
    console.log(result)

  }
})